import moment from 'moment';
const request = ({ url, method = "POST", data }) => {
    //http://zhtyzx.cn/zhh_sports/commonConfig-dateListByItemId.action
    //itemid=30

    let headers = {
        "host": "zhtyzx.cn",
        "Origin": "http://zhtyzx.cn",
        "referer": "http://zhtyzx.cn/zhh_sports/view/item/item_main.jsp",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.63 Safari/537.36",
        "Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
        "X-Requested-With": "XMLHttpRequest"
    }

    return fetch(url, {
        method,
        headers,
        body: data,
        withCredentials: true,
        credentials: "include"
    }).then(res => {
        return res
    })

}


export function getDateList() {
    return request({
        url: "http://zhtyzx.cn/zhh_sports/commonConfig-dateListByItemId.action",
        data: "itemid=30"
    }).then((res) => {
        let result = JSON.parse(res._bodyText).__result__

        let dayCount = result.data[0].length

        let list = []

        for (let i = 0; i < dayCount; i++) {
            list.push({
                date: result.data[0][i],
                day: result.data[1][i],
                enabled: result.data[2][i]
            })
        }

        return list.filter(o => o.enabled)

    })
}

export function getTicks(date) {
    return request({
        url: "http://zhtyzx.cn/zhh_sports/commonConfig-timePeriod.action",
        data: "itemid=30&itemday=" + encodeURIComponent(date)
    }).then((res) => {
        let result = JSON.parse(res._bodyText).__result__
        return [
            {
                site: result.data.siteList[0][0],
                siteName: result.data.siteList[0][1],
                begin: result.data.timeList[0][0],
                end: result.data.timeList[0][1],
                left: result.data.siteTimeList[1][1]
            },
            {
                site: result.data.siteList[0][0],
                siteName: result.data.siteList[0][1],
                begin: result.data.timeList[1][0],
                end: result.data.timeList[1][1],
                left: result.data.siteTimeList[1][2]
            }
        ]
    }).catch(e => {
        console.log(e)
    })
}

export async function sendVerifyCode(props) {
    //{ mobile, actDate, itemId, groundId, startTime, endTime, identityCode, type = 0 }
    try {
        let randRes = await request({
            url: "http://zhtyzx.cn/zhh_sports/orderTicket-getRand.action?r=" + new Date().getTime()
        })

        props.verifyCode = JSON.parse(JSON.parse(randRes._bodyText)).rand
        await request({ url: "http://zhtyzx.cn/zhh_sports/view/item/image.jsp?strRand=" + props.verifyCode })

        props.actDate = moment(props.actDate).format("YYYY-MM-DD")

        let strings = []
        for (let key in props) {
            strings.push(key + "=" + encodeURIComponent(props[key]))
        }

        let postString = strings.join("&")

        let sendRes = await request({
            url: "http://zhtyzx.cn/zhh_sports/commonConfig-checkCode.action",
            data: postString
        })

        let result = JSON.parse(sendRes._bodyText).__result__

        if (result.data.status === "200") {
            return result.data
        }
        else {
            return Promise.reject(result.data)
        }

    }
    catch (e) {
        console.log(e)
    }
}

export async function orderTicket(props) {
    try {
        props["ticketDetial.actDate"] = moment(props["ticketDetial.actDate"]).format("YYYY-MM-DD")

        let strings = []
        for (let key in props) {
            strings.push(key + "=" + encodeURIComponent(props[key]))
        }

        let postString = strings.join("&")

        let sendRes = await request({
            url: "http://zhtyzx.cn/zhh_sports/orderTicket-saveOrderTicket.action",
            data: postString
        })

        let result = JSON.parse(sendRes._bodyText).__result__

        if (result.data.status === "200") {
            return result.data
        }
        else {
            return Promise.reject(result.data)
        }

    }
    catch (e) {
        console.log(e)
    }
}

export async function getMyTicks(idcard) {
    let res = await request({
        url: "http://zhtyzx.cn/zhh_sports/orderTicket-listOrderTicket.action",
        data: "codeNum=" + idcard
    })

    let result = JSON.parse(res._bodyText).__result__

    return result
}

export async function sendRefundMessage(codeNum) {
    let res = await request({
        url: "http://zhtyzx.cn/zhh_sports/orderTicket-refundTicket.action",
        data: "codeNum=" + codeNum
    })

    let result = JSON.parse(res._bodyText).__result__

    if (result.data.status === "200") {
        return result.data
    }
    else {
        return Promise.reject(result.data)
    }
}

export async function refundTicket({ verifyCode, ticketid }) {
    let res = await request({
        url: "http://zhtyzx.cn/zhh_sports/orderTicket-refundTicketSubmit.action",
        data: `ticketid=${ticketid}&checkCodeid=${verifyCode}`
    })
    let result = JSON.parse(res._bodyText).__result__

    if (result.data.status === "200") {
        return result.data
    }
    else {
        return Promise.reject(result.data)
    }
}